/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.entity;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import com.googlecode.technorati4j.util.StringUtils;

/**
 * Response entity for tag API.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 * 
 */
public final class TagResponse
    implements Response {

    /** The query . */
    private String query;

    /** Number of posts that match the tag. */
    private Integer postsMatched;

    /** Number of blogs that match the tag. */
    private Integer blogsMatched;

    /** Value of the start parameter. */
    private Integer start;

    /** Value of limit parameter. */
    private Integer limit;

    /** Duration of query. */
    private Double queryTime;

    /**
     * List of items found.
     */
    private List<TagResponseItem> items;

    @SuppressWarnings("unchecked")
    public TagResponse(Element element) {
        Element result = element.getChild("result");

        query = result.getChildText("query");
        postsMatched = StringUtils.toInteger(result.getChildText("postsmatched"));
        blogsMatched = StringUtils.toInteger(result.getChildText("blogsmatched"));
        start = StringUtils.toInteger(result.getChildText("start"));
        limit = StringUtils.toInteger(result.getChildText("limit"));
        queryTime = StringUtils.toDouble(result.getChildText("querytime"));

        items = new ArrayList<TagResponseItem>();
        List<Element> _items = element.getChildren("item");
        for (Element item : _items) {
            this.items.add(new TagResponseItem(item));
        }
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public Integer getPostsMatched() {
        return postsMatched;
    }

    public void setPostsMatched(Integer postsMatched) {
        this.postsMatched = postsMatched;
    }

    public Integer getBlogsMatched() {
        return blogsMatched;
    }

    public void setBlogsMatched(Integer blogsMatched) {
        this.blogsMatched = blogsMatched;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Double getQueryTime() {
        return queryTime;
    }

    public void setQueryTime(Double queryTime) {
        this.queryTime = queryTime;
    }

    public List<TagResponseItem> getItems() {
        return items;
    }

    public void setItems(List<TagResponseItem> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        final StringBuilder str = new StringBuilder();
        str.append("query=");
        str.append(query);
        str.append("&postsMatched=");
        str.append(postsMatched);
        str.append("&blogsMatched=");
        str.append(blogsMatched);
        str.append("&start=");
        str.append(start);
        str.append("&limit=");
        str.append(limit);
        str.append("&queryTime=");
        str.append(queryTime);
        str.append("&items=");
        str.append(items);

        return str.toString();
    }

}
